﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Threading;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Data;

namespace Kokomo
{
	public class EnhancedTreeView : TreeView
	{
		static EnhancedTreeView()
		{
		}

		public EnhancedTreeView()
		{
		}

		public object SelectedObject
		{
			get { return (object)GetValue(SelectedObjectProperty); }
			set { SetValue(SelectedObjectProperty, value); }
		}

		// Using a DependencyProperty as the backing store for SelectedObject.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty SelectedObjectProperty =
			DependencyProperty.Register("SelectedObject", typeof(object), typeof(EnhancedTreeView), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSelectedObjectChanged));
		static void OnSelectedObjectChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
		{
			((EnhancedTreeView)target).OnSelectedObjectChanged(e);
		}
		protected virtual void OnSelectedObjectChanged(DependencyPropertyChangedEventArgs e)
		{
			TreeViewItem container = (TreeViewItem)this.ItemContainerGenerator.ContainerFromItem(e.NewValue);
			if (
				(container != null)
				&& (!container.IsSelected)
				)
			{
				container.IsSelected = true;
			}
		}

		protected override void OnSelectedItemChanged(RoutedPropertyChangedEventArgs<object> e)
		{
			base.OnSelectedItemChanged(e);

			this.SelectedObject = e.NewValue;
		}

		protected override DependencyObject GetContainerForItemOverride()
		{
			EnhancedTreeViewItem container = new EnhancedTreeViewItem();

			return container;
		}
	}
}
